﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using brockredge.CLASSES;

namespace brockredge
{
    public partial class NewAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }

        protected void signup_Click(object sender, EventArgs e)
        {
            try
            {            
                testDataContext tdc = new testDataContext();
                User u = new User();
                User_Membership um = new User_Membership();

                if (!varifyUserRegistration(reemailtxtbox.Text.ToString()))
                {
                    var fullname = System.Guid.NewGuid().ToString();
                    u.user_id = (Guid)fullname.ToGuid();
                    u.fname = fnametxtbox.Text.ToString();
                    u.lname = lnametxtbox.Text.ToString();
                    u.mobile = "null";
                    tdc.Users.InsertOnSubmit(u);
                    tdc.SubmitChanges();

                    um.user_id = (Guid)fullname.ToGuid();
                    um.email_id = reemailtxtbox.Text.ToString();

                    um.password = StringExt.encryptString(passwordtxtbox.Text);
                    um.createDate = DateTime.Today.Date;
                    um.IsApproved = 0;
                    tdc.User_Memberships.InsertOnSubmit(um);
                    tdc.SubmitChanges();

                    var URL = "http://localhost:2180/ApproveUser.aspx?q=" + StringExt.encryptString(fullname.ToGuid().ToString());
                    SendMailExt.SendEmail("abhishektiwarimrt@gmail.com", "Bca2Ke26", reemailtxtbox.Text, 
                        "Account Created", URL, System.Web.Mail.MailFormat.Text, "");
                }
                else
                {
                    errlbl.Text = "User for this email is already registered";
                }
            }
            catch(Exception ex)
            {
                errlbl.Text=ex.Message;
            }
            
        }

        private Boolean varifyUserRegistration(String email)
        {
            testDataContext tdc = new testDataContext();
            var varifyQ = from user in tdc.User_Memberships
                          where user.email_id == email
                          select user.email_id;
            if (varifyQ.Count() > 0)
                return true;
            else
                return false;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            testDataContext tdc = new testDataContext();
            var validUser = from u in tdc.User_Memberships
                            where u.email_id == emailtxtbox.Text && u.password == StringExt.encryptString(passlogin.Text)
                            select u;
            if (validUser.Count() > 0)
            {
                foreach (var user in validUser)
                {
                    if (user.IsApproved == 1)
                    {
                        Session["userId"] = user.user_id.ToString();
                        Response.Redirect("/Welcome.aspx");
                    }
                    else
                    {
                        errorlbllogin.Text = "Please varify your account";
                    }
                }
            }
            else
            {
                errorlbllogin.Text = "Please Register First";
            }
        }

    }
}